package com.duolaibei.pay.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.duolaibei.pay.R;
import com.duolaibei.pay.base.BaseActivity;
import com.duolaibei.pay.bean.PayOrder;
import com.duolaibei.pay.smprint.AidlUtil;
import com.duolaibei.pay.utils.SharedPreferencesUtils;
import com.duolaibei.pay.utils.TimeUtils;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by wangshuo on 2017/5/15.
 */

public class DealDetailsActivity extends BaseActivity {
    @Bind(R.id.price)
    TextView price;//金额
    @Bind(R.id.business)
    TextView business;//商家
    @Bind(R.id.pay_type)
    TextView payType;//支付类型
    @Bind(R.id.pay_time)
    TextView payTime;//支付时间
    @Bind(R.id.deal_num)
    TextView dealNum;//交易号
    @Bind(R.id.deal_status)
    TextView dealStatus;//交易状态
    @Bind(R.id.print)
    TextView print;//打印
    @Bind(R.id.describe_layout)
    LinearLayout descLayout;//打印
    @Bind(R.id.certificate_layout)
    LinearLayout certificateLayout;
    @Bind(R.id.certificate_number)
    TextView certificateNumber;
    private PayOrder order = null;

    private final String TIME_FORMAT = "yyy/MM/dd HH:mm:ss";
    private int type = 0;
    private int indexType = 0;

    @Override
    public int getContentViewResId() {
        return R.layout.activity_deal_details;
    }

    class PrinterStatusReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {

        }
    }

    @Override
    public void intiViews() {
        Intent intent = getIntent();
        if (null != intent) {
            order = intent.getParcelableExtra("order");
            type = intent.getIntExtra("type", 0);
            indexType = intent.getIntExtra("index", 0);
        }
        if (indexType == TransactionActivity.INDEX_TYPE) {
            certificateLayout.setVisibility(View.GONE);
        } else if (indexType == BrushDetailActivity.INDEX_TYPE) {
            certificateLayout.setVisibility(View.VISIBLE);
        }
        print.setOnClickListener(this);
        bindView();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public String setTitle() {
        return getString(R.string.deal_details_text);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        int id = view.getId();
        if (id == R.id.print) {
            //打印
            smPrint();
        }
    }

    private void bindView() {
        certificateNumber.setText(order.getTrade_no());
        price.setText(getString(R.string.price, order.getTotal_amount()));
        business.setText(order.getBody());
        payType.setText(order.getPay_name());
        dealNum.setText(order.getOut_trade_no());
        payTime.setText(TimeUtils.stampToDate(order.getPaytime() + "000"));
        if (order.getStatus().equals("1")) {
            dealStatus.setText(R.string.deal_succ);
            print.setVisibility(View.VISIBLE);
            price.setTextColor(ContextCompat.getColor(this, R.color.common_title_color));
        } else if (order.getStatus().equals("-1")) {
            dealStatus.setText(R.string.refunded);
            print.setVisibility(View.GONE);
            descLayout.setVisibility(View.GONE);
            price.setTextColor(ContextCompat.getColor(this, R.color.refund_color));
            dealStatus.setTextColor(ContextCompat.getColor(this, R.color.refund_color));
        } else if (order.getStatus().equals("0")) {
            dealStatus.setText(R.string.no_pay);
        }
        if (type == 200) {
            smPrint();
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();

            }
            smPrint();
        }
    }

    /**
     * 商米打印
     */
    private void smPrint() {
        //初始化打印服务
        if (AidlUtil.getInstance().isConnect()) {
            AidlUtil.getInstance().initPrinter();
        }
        TelephonyManager tm = (TelephonyManager) getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
        //标题
        AidlUtil.getInstance().printText(getString(R.string.print_title) + "\n", 50);
        //电话
        AidlUtil.getInstance().printText(getString(R.string.print_title_phone) + "\n");
        //存根
        AidlUtil.getInstance().printText(getString(R.string.shop_save) + "\n", 20, 2);
        //分割线
        AidlUtil.getInstance().printText("-----------------------------" + "\n");
        //订单金额
        AidlUtil.getInstance().printText(getString(R.string.order_money, order.getTotal_amount()) + "\n");
        //消费金额
        AidlUtil.getInstance().printText(getString(R.string.shop_money, order.getPay_amount()) + "\n");
        //优惠金额
        AidlUtil.getInstance().printText(getString(R.string.discountable_money, order.getDiscountable_amount()) + "\n");
        //分割线
        AidlUtil.getInstance().printText("-----------------------------" + "\n");
        //交易时间
        AidlUtil.getInstance().printText(getString(R.string.pay_time, TimeUtils.stampToDate(order.getPaytime() + "000", TIME_FORMAT)) + "\n");
        //交易渠道
        AidlUtil.getInstance().printText(getString(R.string.pay_name, order.getPay_name()) + "\n");
        //交易描述
        AidlUtil.getInstance().printText(getString(R.string.pay_describe, order.getBody()) + "\n");
        //终端编号
        AidlUtil.getInstance().printText(getString(R.string.terminal_num, SharedPreferencesUtils.getTerminalNumber()) + "\n");
        //订单编号
        AidlUtil.getInstance().printText(getString(R.string.device_num, tm.getDeviceId()) + "\n");
        if (indexType == BrushDetailActivity.INDEX_TYPE) {
            //凭证号
            AidlUtil.getInstance().printText(getString(R.string.certificate_number, order.getTrade_no()) + "\n");
        }
        //订单号
        AidlUtil.getInstance().printText(getString(R.string.order_num, order.getOut_trade_no()+ "\n"));
        //打印条码
//        AidlUtil.getInstance().printBarCode(order.getOut_trade_no(), 5, 65, 2, 0);
//        AidlUtil.getInstance().printBarCode(order.getTrade_no(), 8, 65, 2, 0);
        AidlUtil.getInstance().printQr(order.getOut_trade_no(), 5, 2);
        //换行
        AidlUtil.getInstance().printText("\n");
        //备注
        AidlUtil.getInstance().printText(getString(R.string.ps) + "\n");
        //用户签名
        AidlUtil.getInstance().printText(getString(R.string.user_name) + "\n");
        //空打五行
        AidlUtil.getInstance().print5Line();
    }
}
